package com.cgbl.cg.vm.home

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableField
import api.*
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.*
import com.robot.base.util.LiveDataBus
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class HomeRecommendViewModel(application: Application) : GLBaseViewModel(application) {

    /**
     * 课程热门推荐，后台课程列表配置，课程分发
     * 为热门推荐，默认如果未获取到已上架的课程配置，或获取少于2个，则整个区域不显示
     */
    val recommendCourseVisit = ObservableField(true)

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val workDataEvent = SingleLiveEvent<List<WorksBean>>()
        val courseDataEvent = SingleLiveEvent<List<CourseBean>>()
        val ribbonDataEvent = SingleLiveEvent<List<BannerBean>>()
        val refreshEvent = SingleLiveEvent<Int>()
        val limitedDataEvent = SingleLiveEvent<List<PreferentPageBean>>()
        val projectDataEvent = SingleLiveEvent<List<ProjectBean>>()
        val resourceDataEvent = SingleLiveEvent<List<ResourceBean>>()
    }

    val toWorksListCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.WORK.A_WORKS_LIST)
    })

    val toRecommendMasterCourseCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.HOME.A_RECOMMEND_MASTER_COURSE,
            Bundle().apply {
                putInt(
                    AppConstants.BundleKey.RECOMMEND_TYPE, AppConstants.BundleValue.MASTER_COURSE
                )
            })
    })

    val toClassCaseCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.HOME.A_RECOMMEND_MASTER_COURSE,
            Bundle().apply {
                putInt(
                    AppConstants.BundleKey.RECOMMEND_TYPE, AppConstants.BundleValue.CLASS_CASE
                )
            })
    })

    val toPopularResourcesCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.HOME.A_RECOMMEND_MASTER_COURSE,
            Bundle().apply {
                putInt(
                    AppConstants.BundleKey.RECOMMEND_TYPE,
                    AppConstants.BundleValue.POPULAR_RESOURCES
                )
            })
    })

    val toRecommendVipCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.HOME.A_RECOMMEND_MASTER_COURSE,
            Bundle().apply {
                putInt(
                    AppConstants.BundleKey.RECOMMEND_TYPE, AppConstants.BundleValue.VIP
                )
            })
    })

    val toCourseCommand = BindingCommand<Any>(BindingAction {
        LiveDataBus.get().with(AppConstants.LiveKey.HOME_TAB).postValue(3)
    })

    val toProjectCommand = BindingCommand<Any>(BindingAction {
        LiveDataBus.get().with(AppConstants.LiveKey.HOME_TAB).postValue(5)
    })

    val toResourceCommand = BindingCommand<Any>(BindingAction {
        LiveDataBus.get().with(AppConstants.LiveKey.HOME_TAB).postValue(4)
    })

    val toWorkCommand = BindingCommand<Any>(BindingAction {
        LiveDataBus.get().with(AppConstants.LiveKey.HOME_TAB).postValue(2)
    })

    val toLimitedTimeCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.HOME.A_LIMITED_TIME)
    })

    fun refresh() {
        getRecommendCourse()
        getRecommendWork()
        getRibbonList()
        getPreferentPage()
        casePage()
    }

    fun getRecommendWork() {
        WorkServiceFactory.page(
            mapOf(
                "searchType" to 0, "pageNo" to 1, "pageSize" to 3, "isHot" to 1, "isAccount" to 0
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<WorksBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<WorksBean>>) {
                uc.refreshEvent.postValue(0)
                uc.workDataEvent.postValue(t.data.list)
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(0)
            }
        })
    }

    fun getRecommendCourse() {
        CourseServiceFactory.page(
            mapOf(
                "sortType" to 0,
                "pageNo" to 1,
                "pageSize" to 4,
                "classRecommend" to 3,
                "isAccount" to 0
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<CourseBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<CourseBean>>) {
                uc.refreshEvent.postValue(0)
                uc.courseDataEvent.postValue(t.data.list)
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(0)
            }
        })
    }

    fun getRibbonList() {
        UserServiceFactory.getBannerList(
            0
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<BannerBean>>>(this) {
            override fun onNext(t: BaseResponse<List<BannerBean>>) {
                uc.refreshEvent.postValue(0)
                uc.ribbonDataEvent.postValue(t.data)
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(0)
            }
        })
    }

    fun getPreferentPage() {
        UserServiceFactory.getPreferentPage(
            mapOf(
                "pageNo" to 1, "pageSize" to 10, "sort" to "0"
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<PreferentPageBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<PreferentPageBean>>) {
                uc.limitedDataEvent.postValue(t.data.list)
            }
        })
    }

    fun casePage() {
        ProjectServiceFactory.casePage(
            mapOf(
                "pageNo" to 1,
                "pageSize" to 3,
                "classRecommend" to 1
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ProjectBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ProjectBean>>) {
                uc.projectDataEvent.postValue(t.data.list)
            }
        })
    }

    fun resourcePage() {
        ResourceServiceFactory.page(
            mapOf(
                "pageNo" to 1,
                "pageSize" to 2,
                "naturalRecommend" to 4,
                "isAccount" to 0
            )
        ).subscribe(object :AbstractViewModelSubscriber<BaseResponse<PageListBean<ResourceBean>>>(this){
            override fun onNext(t: BaseResponse<PageListBean<ResourceBean>>) {
                uc.resourceDataEvent.postValue(t.data.list)
            }
        })
    }
}